import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import Users from '@/components/users/Users'
import Roles from '@/components/power/Roles'
import Rights from '@/components/power/Rights'
import Categories from '@/components/goods/Categories'
import Goods from '@/components/Goods'
import Orders from '@/components/Orders'
import Params from '@/components/goods/Params'
import Reports from '@/components/Reports'
import {
  Form, FormItem, Input, Button, Message, Container, Header, Aside, Main, Menu, Submenu, MenuItemGroup, MenuItem, Breadcrumb, BreadcrumbItem,
  Card, Row, Col, Table, TableColumn, Switch, Tooltip, Pagination, Dialog, Popconfirm, MessageBox, Tag, Tree, Select, Option, Cascader,
  Alert, Tabs, TabPane
} from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(VueRouter)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Button)
Vue.use(Container)
Vue.use(Header)
Vue.use(Aside)
Vue.use(Main)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItemGroup)
Vue.use(MenuItem)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Card)
Vue.use(Row)
Vue.use(Col)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Switch)
Vue.use(Tooltip)
Vue.use(Pagination)
Vue.use(Dialog)
Vue.use(Popconfirm)
Vue.use(Tag)
Vue.use(Tree)
Vue.use(Select)
Vue.use(Option)
Vue.use(Cascader)
Vue.use(Alert)
Vue.use(Tabs)
Vue.use(TabPane)

Vue.prototype.$message = Message
Vue.prototype.$confirm = MessageBox

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/rights', component: Rights },
      { path: '/categories', component: Categories },
      { path: '/goods', component: Goods },
      { path: '/orders', component: Orders },
      { path: '/params', component: Params },
      { path: '/reports', component: Reports }
    ]
  }
]

const guard = (to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转过来
  // next是一个函数，表示放行
  // next() 放行    next('/home') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) next('/login')
  // 放行
  next()
}

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach(guard)

export default router
